package 实验三.三层cs;// ... (省略了import语句和Socket通信代码，与原始Server类相似)
import java.io.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class ApplicationServer {
    public static void main(String[] args) {  
        // ... (Socket通信代码，与原始Server类的main方法相似)  
        ServerSocket serverSocket = null;
        Socket socket = null;
        BufferedReader in = null;
        PrintWriter out = null;
        try {  
            // ... (Socket初始化代码)  
            serverSocket = new ServerSocket(12345);
            System.out.println("服务器启动，等待客户端连接...");

            socket = serverSocket.accept();
            System.out.println("客户端已连接");

            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(), true);

            String inputLine;
            String response = ""; // 给客户端的响应  
            while ((inputLine = in.readLine()) != null) {  
                String[] parts = inputLine.split("-");  
                String action = parts[0];  
  
                switch (action) {  
                    case "1":  
                        addContact(new Contact(parts[1], parts[2], parts[3]));  
                        response = "添加成功";  
                        break;  
                    case "2":  
                        response = DataServer.modifyContact(parts[1], parts[2], parts[3]);  
                        break;  
                    case "3":  
                        deleteContact(parts[1]);  
                        response = "删除成功";  
                        break;  
                    case "4":  
                        out.println(DataServer.viewContact());  
                        response = "查询成功";  
                        break;  
                    default:  
                        System.out.println("未知操作：" + inputLine);  
                        break;  
                }  
                out.println(response);  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {
            try {
                if (in != null) in.close();
                if (out != null) out.close();
                if (socket != null) socket.close();
                if (serverSocket != null) serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }  
    }  
  
    private static void addContact(Contact contact) {  
        DataServer.addContact(contact);  
    }  
  
    private static void deleteContact(String name) {  
        DataServer.deleteContact(name);  
    }  
  
    // 注意：modifyContact方法在这里不需要额外实现，因为它直接调用了DataServer的静态方法  
}